﻿using UnityEngine;

namespace Tools.Unilts
{
    public static class AnimationExt
    {
        /// <summary>
    /// 检测动画标签
    /// </summary>
    /// <param name="animator"></param>
    /// <param name="tagName"></param>
    /// <param name="animationIndex"></param>
    /// <returns></returns>
    public static bool CheckAnimationTag(this Animator animator, string tagName, int animationIndex = 0)
    {
        return animator.GetCurrentAnimatorStateInfo(animationIndex).IsTag(tagName);
    }

    /// <summary>
    /// 检测动画片段名称
    /// </summary>
    /// <param name="animator"></param>
    /// <param name="animationName"></param>
    /// <param name="animationIndex"></param>
    /// <returns></returns>
    public static bool CheckAnimationName(this Animator animator, string animationName, int animationIndex = 0)
    {
        return animator.GetCurrentAnimatorStateInfo(animationIndex).IsName(animationName);
    }
    
    public static float AnimationNormalTime(this Animator animator, int layer = 0)
    {
        return animator.GetCurrentAnimatorStateInfo(layer).normalizedTime;
    }

    public static bool CheckCurrentTagAnimationTimeIsLess(this Animator animator, string tagName, float time)
    {
        if (animator.CheckAnimationTag(tagName))
        {
            //如果当前动画状态的标准化时间小于传递的时间返回true 否则返回false
            return (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < time);
        }

        //默认返回false
        return false;
    }

    public static bool CheckCurrentTagAnimationTimeIsExceed(this Animator animator, string tagName, float time)
    {
        if (animator.CheckAnimationTag(tagName))
        {
            //如果当前动画状态的标准化时间大于传递的时间返回true 否则返回false
            return (animator.GetCurrentAnimatorStateInfo(0).normalizedTime > time);
        }

        //默认返回false
        return false;
    }

    /// <summary>
    /// 检查当前动画是否播放完毕
    /// </summary>
    /// <param name="animator"></param>
    /// <param name="startTime"></param>
    /// <returns></returns>
    public static bool CheckCurrentTagAnimationIsFinished(this Animator animator, float startTime)
    {
        var durationTime = Time.time - startTime;
        //如果当前动画状态的标准化时间大于传递的时间返回true 否则返回false
        return (animator.GetCurrentAnimatorStateInfo(0).length <= durationTime);
    }

    }
}